﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Phone.Controls;
using Microsoft.Unsupported;
using System.ComponentModel;
using Util.ViewModels;
using Util.Models;
using Domain;

namespace ScotiabankWP7.Forms
{
    public partial class MenuPanorama : PhoneApplicationPage
    {

        PickerBoxDialog dialog;
        PickerBoxDialog dialogCuentaCargoTC;    

         
        private string[] data = null;
        private int selectedIndex;

        public MenuPanorama()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MenuPanorama_Loaded);
            
            this.listPicker2.ItemsSource = GetEmpresasTelefonicas();
            this.listPicker2.SelectedIndex = 0;

            this.listPickerTarjeta.ItemsSource = GetTarjetas();
            this.listPickerTarjeta.SelectedIndex = 0;

            TiltEffect.SetIsTiltEnabled(this, true);

            data = GetCuentasArray();
            this.bCuentas.DataContext = data[0];
            this.bCuentaCargo.DataContext = data[0];

            InitPickerBoxDialog();
            InitPickerBoxDialogCtaCargoTC();

        }

        private void InitPickerBoxDialog()
        {
            dialog = new PickerBoxDialog();
            // Assign data source and title
            dialog.ItemSource = data;
            dialog.Title = "cuentas bancarias";
            // Hook up into closed event
            dialog.Closed += new EventHandler(dialog_Closed);
        }

        private void InitPickerBoxDialogCtaCargoTC()
        {
            dialogCuentaCargoTC = new PickerBoxDialog();
            dialogCuentaCargoTC.ItemSource = data;
            dialogCuentaCargoTC.Title = "cuentas bancarias";
            dialogCuentaCargoTC.Closed += new EventHandler(dialogCtaCargoTC_Closed);
        }

        void dialog_Closed(object sender, EventArgs e)
        {
            selectedIndex = dialog.SelectedIndex;            
            this.bCuentas.DataContext = data[selectedIndex];
        }

        void dialogCtaCargoTC_Closed(object sender, EventArgs e)
        {
            selectedIndex = dialogCuentaCargoTC.SelectedIndex;            
            this.bCuentaCargo.DataContext = data[selectedIndex];
        }

        private IList<string> GetCuentas()
        {
            List<string> items = new List<string>();
            items.Add("AHMN 123-4567890     S/. 250.55");
            items.Add("AHME 000-4588899     S/. 580.00");
            items.Add("AHME 000-4588899     S/. 1580.00");
            items.Add("AHME 000-4588899     S/. 480.00");
            items.Add("AHME 000-4588899     S/. 45880.00");
            //items.Add("AHME 000-4588899 S/. 896.00");
            //items.Add("AHME 000-4588899 S/. 1234.00");
            //items.Add("AHME 000-4588899 S/. 6696.00");
            //items.Add("AHME 000-4588899 S/. 15900.00");
            //items.Add("AHME 000-4588899 S/. 82.00");
            //items.Add("AHME 000-4588899 S/. 77.00");
            return items;
        }

        private string[] GetCuentasArray()
        {
            string[] data = new string[10];
            data[0] = "AHMN 123-4567890     S/. 250.55";
            data[1] = "AHME 000-4588899     S/. 580.00";
            data[2] = "AHME 000-4588899     S/. 1580.00";
            data[3] = "AHME 000-4588899     S/. 480.00";
            data[4] = "AHME 000-4588899     S/. 45880.00";
            data[5] = "AHME 000-4588899     S/. 896.00";
            data[6] = "AHME 000-4588899     S/. 1234.00";
            data[7] = "AHME 000-4588899     S/. 6696.00";
            data[8] = "AHME 000-4588899     S/. 15900.00";
            data[9] = "AHME 000-4588899     S/. 82.00";
            return data;
        }

        private IList<string> GetEmpresasTelefonicas()
        {
            List<string> items = new List<string>();
            items.Add("<Seleccione>");
            items.Add("Claro");
            items.Add("Movistar");
            items.Add("Nextel");
            return items;
        }

        private IList<string> GetTarjetas()
        {
            List<string> items = new List<string>();            
            items.Add("MN:5549110910001264 S/.");
            items.Add("MN:5142610911156264 $/.");
            items.Add("MN:5844177910103324 S/.");
            return items;
        }


        void MenuPanorama_Loaded(object sender, RoutedEventArgs e)
        {
            List<Cuenta> listaCuentas = new List<Cuenta>();
            listaCuentas.Add(new Cuenta("Ahorros", "444-444444", "S/.20.56"));
            listaCuentas.Add(new Cuenta("Ahorros", "555-555555", "S/.1530.45"));
            CuentasList.ItemsSource = listaCuentas;

            List<TarjetaCredito> listaTarjetas = new List<TarjetaCredito>();
            listaTarjetas.Add(new TarjetaCredito("Visa Platinium Dolares", "4948 2356 1245 0002", "US$", "1256.23", "US$ 1256.23"));
            listaTarjetas.Add(new TarjetaCredito("Mastercard Internacional", "4948 2356 1245 0002", "S/.", "1256.23", "S/. 1256.23"));
            TarjetasList.ItemsSource = listaTarjetas;           

        }
        
        private void MovimientosCuenta_Click(object sender, RoutedEventArgs e)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.Navigate(new Uri("/Forms/Cuentas/MovimientosCuenta.xaml", UriKind.Relative));

        }
       
        private void Recargar_Click(object sender, RoutedEventArgs e)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.Navigate(new Uri("/Forms/Recarga/RecargaResultado.xaml", UriKind.Relative));            
        }

        private void SeleccionarCuenta(object sender, RoutedEventArgs e)
        {
            //ListBoxItem selectedItem = lbCuentas.SelectedItem as ListBoxItem;
            //bCtaCargo.Content = selectedItem.Content;
            //lbCuentas.Visibility = Visibility.Collapsed;
            //bCtaCargo.Visibility = Visibility.Visible;
        }

        private void bCuentas_Click(object sender, RoutedEventArgs e)
        {            
            dialog.SelectedIndex = selectedIndex;
            dialog.Show();
        }

        private void bCuentaCargo_Click(object sender, RoutedEventArgs e)
        {
            dialogCuentaCargoTC.SelectedIndex = selectedIndex;
            dialogCuentaCargoTC.Show();
        }
                
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            // Dehabilita el boton Back
            e.Cancel = true;
        }

        #region Evento del menu Transferencias

        private void listaTransferencias_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int seleccionado = listaTransferencias.SelectedIndex;

            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;

            listaTransferencias.SelectedIndex = -1;
            // Pago de Otras Instituciones
            if (seleccionado == 0)
            {
                root.Navigate(new Uri("/Forms/Transferencias/TransfCtaPropia.xaml", UriKind.Relative));
            }
            else if (seleccionado == 1)
            {
                root.Navigate(new Uri("/Forms/Transferencias/TransfCtaTercero.xaml", UriKind.Relative));
            }
        }

        #endregion

        #region Evento Click del Boton Pagar Tarjeta de Crédito

        private void btnPagar_Click(object sender, RoutedEventArgs e)
        {
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            root.Navigate(new Uri("/Forms/TarjetaCredito/PagoTarjetaCreditoOperacionRealizada.xaml", UriKind.Relative));
        }

        #endregion

        #region Evento del menu Pago de Servicios
        private void listaServicios_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int seleccionado = listaServicios.SelectedIndex;

            listaServicios.SelectedIndex = -1;

            if (seleccionado == 0)
            {
                PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
                root.Navigate(new Uri("/Forms/PagoServicios/BuscaAguaLuzGas.xaml", UriKind.Relative));
            }

            else if (seleccionado == 1)
            {
                PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
                root.Navigate(new Uri("/Forms/PagoServicios/BuscaTelfCableInternet.xaml", UriKind.Relative));
            }

            else if (seleccionado == 2)
            {
                PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
                root.Navigate(new Uri("/Forms/PagoServicios/BuscaOtrasInstituciones.xaml", UriKind.Relative));
            }

            // Falta Implementar esto
            else if (seleccionado == 3)
            {
                PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
                root.Navigate(new Uri("/Forms/PagoServicios/PagosFrecuentes.xaml", UriKind.Relative));
            }
        }

        #endregion

        #region Metodo que maneja la navegacion desde el application bar

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (NavigationContext.QueryString.Count > 0)
            {
                string indice = NavigationContext.QueryString["indice"];

                if (String.IsNullOrEmpty(indice).Equals(false))
                {
                    PanoramaContent.DefaultItem = PanoramaContent.Items[Convert.ToInt16(indice)];                    
                    base.OnNavigatedTo(e);
                }
            }
        }

        #endregion
    }
}